
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "ClientProfileDlg.h"
//Sulan start svn 163
#include "../client/RegexpHandler.h"
//End


#define GET_TEXT(id, var) \
	GetDlgItemText(id, buf, 1024); \
	var = buf;

#define ATTACH(id, var) var.Attach(GetDlgItem(id))

LRESULT ClientProfileDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	if(currentProfileId != -1) {
		// FIXME disable this for now to stop potential dupes (ahh. fuck it, leave it enabled :p)
		//::EnableWindow(GetDlgItem(IDC_CLIENT_NAME), false);
		adding = false;
		getProfile();
	} else {
		adding = true;
		::EnableWindow(GetDlgItem(IDC_NEXT), false);
		//Sulan start svn77
		::EnableWindow(GetDlgItem(IDC_BACK), false);
		//End
	}

	ATTACH(IDC_CLIENT_NAME, ctrlName);
	ATTACH(IDC_CLIENT_VERSION, ctrlVersion);
	ATTACH(IDC_CLIENT_TAG, ctrlTag);
	ATTACH(IDC_CLIENT_EXTENDED_TAG, ctrlExtendedTag);
	ATTACH(IDC_CLIENT_LOCK, ctrlLock);
	ATTACH(IDC_CLIENT_PK, ctrlPk);
	ATTACH(IDC_CLIENT_SUPPORTS, ctrlSupports);
	ATTACH(IDC_CLIENT_TESTSUR_RESPONSE, ctrlTestSUR);
	ATTACH(IDC_CLIENT_USER_CON_COM, ctrlUserConCom);
	ATTACH(IDC_CLIENT_STATUS, ctrlStatus);
	ATTACH(IDC_CLIENT_CHEATING_DESCRIPTION, ctrlCheatingDescription);
	ATTACH(IDC_CLIENT_CONNECTION, ctrlConnection);

	ATTACH(IDC_USE_EXTRA_VERSION, ctrlUseExtraVersion);
	ATTACH(IDC_VERSION_MISMATCH, ctrlCheckMismatch);
	ATTACH(IDC_RECHECK, ctrlRecheck);
	ATTACH(IDC_SKIP_EXTENDED, ctrlSkipExtended);
	ATTACH(IDC_SKIP_EXTENDED, ctrlSkipExtended2);

	ATTACH(IDC_ADD_LINE, ctrlAddLine);
	ATTACH(IDC_CLIENT_FORMATTED_TAG, ctrlFormattedTag);
	ATTACH(IDC_COMMENT, ctrlComment);

	ATTACH(IDC_CLIENT_PROFILE_RAW, ctrlRaw);
	//Sulan start svn209
	for(int n=0;n<11;++n){
		ctrlRaw.AddString(RAWSETTING(n).c_str());
	}
	//End

	ATTACH(IDC_REGEXP_TESTER_COMBO, ctrlRegExpCombo);
	ctrlRegExpCombo.AddString(CTSTRING(VERSION));
	ctrlRegExpCombo.AddString(CTSTRING(TAG));
	ctrlRegExpCombo.AddString(CTSTRING(DESCRIPTION));
	ctrlRegExpCombo.AddString(CTSTRING(LOCK));
	ctrlRegExpCombo.AddString(CTSTRING(PK));
	ctrlRegExpCombo.AddString(CTSTRING(SUPPORTS));
	ctrlRegExpCombo.AddString(CTSTRING(TEST_SUR));
	ctrlRegExpCombo.AddString(CTSTRING(COMMANDS));
	ctrlRegExpCombo.AddString(CTSTRING(STATUS));
	ctrlRegExpCombo.AddString(CTSTRING(CONNECTION));
	ctrlRegExpCombo.SetCurSel(0);

	SetDlgItemText(IDC_CLIPBOARD_ONE, SETTING(CLIPBOARD_NAME_ONE).c_str());
	SetDlgItemText(IDC_CLIPBOARD_TWO, SETTING(CLIPBOARD_NAME_TWO).c_str());
	SetDlgItemText(IDC_CLIPBOARD_THREE, SETTING(CLIPBOARD_NAME_THREE).c_str());
	SetDlgItemText(IDC_CLIPBOARD_FOUR, SETTING(CLIPBOARD_NAME_FOUR).c_str());
	SetDlgItemText(IDC_CLIPBOARD_FIVE, SETTING(CLIPBOARD_NAME_FIVE).c_str());
	SetDlgItemText(IDC_CLIPBOARD_SIX, SETTING(CLIPBOARD_NAME_SIX).c_str());

	params = ClientProfileManager::getInstance()->getParams();

	updateControls();
	
	CenterWindow(GetParent());
	return FALSE;
}

LRESULT ClientProfileDlg::onChange(WORD , WORD , HWND , BOOL& ) {
	updateAddLine();
	return 0;
}

LRESULT ClientProfileDlg::onChangeTag(WORD , WORD , HWND , BOOL& ) {
	updateAddLine();
	updateTag();
	return 0;
}

void ClientProfileDlg::updateTag() {
	TCHAR buf[BUF_LEN];
	string exp;
	GET_TEXT(IDC_CLIENT_TAG, exp); 
	formattedTag = RegexpHandler::formatRegExp(exp, params);
	ctrlFormattedTag.SetWindowText(formattedTag.c_str());
}

void ClientProfileDlg::updateAddLine() {
	addLine = Util::emptyString;
	TCHAR buf[BUF_LEN];

#define UPDATE \
	GetWindowText(buf, BUF_LEN-1); \
	addLine += buf; \
	addLine += ';';

#define BOOL_UD \
	GetWindowText(buf, BUF_LEN-1); \
	addLine += buf; \
	addLine += ';';
	
	ctrlName.UPDATE;
	ctrlVersion.UPDATE;
	ctrlTag.UPDATE;
	ctrlExtendedTag.UPDATE;
	ctrlLock.UPDATE;
	ctrlPk.UPDATE;
	ctrlSupports.UPDATE;
	ctrlTestSUR.UPDATE;
	ctrlUserConCom.UPDATE;
	ctrlStatus.UPDATE;
	ctrlConnection.UPDATE;
	//Sulan start svn93
	ctrlCheatingDescription.UPDATE;

	//useExtraVersion = ctrlUseExtraVersion.GetCheck() == BST_CHECKED;
	//checkMismatch = ctrlCheckMismatch.GetCheck() == BST_CHECKED;
	//recheck = ctrlRecheck.GetCheck() == BST_CHECKED;
	//skipExtended = ctrlSkipExtended.GetCheck() == BST_CHECKED;

	ctrlComment.UPDATE;
	addLine += Util::toString(ctrlUseExtraVersion.GetCheck()) + ';';
	addLine += Util::toString(ctrlCheckMismatch.GetCheck()) + ';';
	addLine += Util::toString(ctrlRecheck.GetCheck()) + ';';
	addLine += Util::toString(ctrlSkipExtended.GetCheck())+ ';';
	addLine += Util::toString(ctrlSkipExtended2.GetCheck());
	//End
	ctrlAddLine.SetWindowText(addLine.c_str());
}

void ClientProfileDlg::getProfile() {
	ClientProfileManager::getInstance()->getClientProfile(currentProfileId, currentProfile);

	name = currentProfile.getName();
	version = currentProfile.getVersion();
	tag = currentProfile.getTag();
	extendedTag = currentProfile.getExtendedTag();
	lock = currentProfile.getLock();
	pk = currentProfile.getPk();
	supports = currentProfile.getSupports();
	testSUR = currentProfile.getTestSUR();
	userConCom = currentProfile.getUserConCom();
	status = currentProfile.getStatus();
	cheatingDescription = currentProfile.getCheatingDescription();
	rawToSend = currentProfile.getRawToSend();
//	tagVersion = currentProfile.getTagVersion();
	useExtraVersion = currentProfile.getUseExtraVersion();
	checkMismatch = currentProfile.getCheckMismatch();
	connection = currentProfile.getConnection();
	comment = currentProfile.getComment();
	recheck = currentProfile.getRecheck();
	skipExtended = currentProfile.getSkipExtended();
	skipExtended2 = currentProfile.getSkipExtended2();
}

void ClientProfileDlg::updateVars() {
	TCHAR buf[1024];

	GET_TEXT(IDC_CLIENT_NAME, name);
	GET_TEXT(IDC_CLIENT_VERSION, version);
	GET_TEXT(IDC_CLIENT_TAG, tag);
	GET_TEXT(IDC_CLIENT_EXTENDED_TAG, extendedTag);
	GET_TEXT(IDC_CLIENT_LOCK, lock);
	GET_TEXT(IDC_CLIENT_PK, pk);
	GET_TEXT(IDC_CLIENT_SUPPORTS, supports);
	GET_TEXT(IDC_CLIENT_TESTSUR_RESPONSE, testSUR);
	GET_TEXT(IDC_CLIENT_USER_CON_COM, userConCom);
	GET_TEXT(IDC_CLIENT_STATUS, status);
	GET_TEXT(IDC_CLIENT_CHEATING_DESCRIPTION, cheatingDescription);
	GET_TEXT(IDC_CLIENT_CONNECTION, connection);
	GET_TEXT(IDC_COMMENT, comment);
	useExtraVersion = ctrlUseExtraVersion.GetCheck() == BST_CHECKED;
	checkMismatch = ctrlCheckMismatch.GetCheck() == BST_CHECKED;
	recheck = ctrlRecheck.GetCheck() == BST_CHECKED;
	skipExtended = ctrlSkipExtended.GetCheck() == BST_CHECKED;
	skipExtended2 = ctrlSkipExtended2.GetCheck() == BST_CHECKED;

	rawToSend = ctrlRaw.GetCurSel();
}

void ClientProfileDlg::updateControls() {
	ctrlName.SetWindowText(name.c_str());
	ctrlVersion.SetWindowText(version.c_str());
	ctrlTag.SetWindowText(tag.c_str());
	ctrlExtendedTag.SetWindowText(extendedTag.c_str());
	ctrlLock.SetWindowText(lock.c_str());
	ctrlPk.SetWindowText(pk.c_str());
	ctrlSupports.SetWindowText(supports.c_str());
	ctrlTestSUR.SetWindowText(testSUR.c_str());
	ctrlUserConCom.SetWindowText(userConCom.c_str());
	ctrlStatus.SetWindowText(status.c_str());
	ctrlCheatingDescription.SetWindowText(cheatingDescription.c_str());
	ctrlAddLine.SetWindowText(addLine.c_str());
	ctrlFormattedTag.SetWindowText(formattedTag.c_str());
	ctrlConnection.SetWindowText(connection.c_str());
	ctrlComment.SetWindowText(comment.c_str());

	ctrlUseExtraVersion.SetCheck(useExtraVersion ? BST_CHECKED : BST_UNCHECKED);
	ctrlCheckMismatch.SetCheck(checkMismatch ? BST_CHECKED : BST_UNCHECKED);
	ctrlRecheck.SetCheck(recheck ? BST_CHECKED : BST_UNCHECKED);
	ctrlSkipExtended.SetCheck(skipExtended ? BST_CHECKED : BST_UNCHECKED);
	ctrlSkipExtended2.SetCheck(skipExtended2 ? BST_CHECKED : BST_UNCHECKED);
	
	ctrlRaw.SetCurSel(rawToSend);
	ctrlName.SetFocus();
}

LRESULT ClientProfileDlg::onNext(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {

	updateVars();

	currentProfile.setName(name);
	currentProfile.setVersion(version);
	currentProfile.setTag(tag);
	currentProfile.setExtendedTag(extendedTag);
	currentProfile.setLock(lock);
	currentProfile.setPk(pk);
	currentProfile.setSupports(supports);
	currentProfile.setTestSUR(testSUR);
	currentProfile.setUserConCom(userConCom);
	currentProfile.setStatus(status);
	currentProfile.setCheatingDescription(cheatingDescription);
	currentProfile.setRawToSend(rawToSend);
//	currentProfile.setTagVersion(tagVersion);
	currentProfile.setUseExtraVersion(useExtraVersion);
	currentProfile.setCheckMismatch(checkMismatch);
	currentProfile.setConnection(connection);
	currentProfile.setComment(comment);
	currentProfile.setRecheck(recheck);
	currentProfile.setSkipExtended(skipExtended);
	currentProfile.setSkipExtended2(skipExtended2);
	ClientProfileManager::getInstance()->updateClientProfile(currentProfile);

	//Sulan start svn77
		dcdebug(" Current Profile1 %s\n", Util::toString(currentProfileId).c_str());
	if(wID == IDC_NEXT && currentProfileId < itemCount-1)
		currentProfileId++;
	else if( wID == IDC_BACK && currentProfileId > 0)
		currentProfileId--;
	dcdebug(" Current Profile2 %s\n", Util::toString(currentProfileId).c_str());
	//End

	getProfile();
	updateControls();

	return 0;
}
//Sulan start 33
LRESULT ClientProfileDlg::onClip(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	switch(wID){
		case IDC_CLIPBOARD_ONE:		WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_ONE));		break;
		case IDC_CLIPBOARD_TWO:		WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_TWO));		break;
		case IDC_CLIPBOARD_THREE:	WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_THREE));	break;
		case IDC_CLIPBOARD_FOUR:	WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_FOUR));	break;
		case IDC_CLIPBOARD_FIVE:	WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_FIVE));	break;
		case IDC_CLIPBOARD_SIX:		WinUtil::setClipboard(SETTING(CLIPBOARD_TEXT_SIX));		break;
	}
	return 0;
}

LRESULT ClientProfileDlg::onMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	TCHAR buf[1024];
	tstring exp, text;
	GET_TEXT(IDC_REGEXP_TESTER_TEXT, text);
	switch(ctrlRegExpCombo.GetCurSel()) {
		case VERSION:	GET_TEXT(IDC_CLIENT_VERSION, exp);	break;
		case TAG: 
			{
				tstring version, versionExp, verTagExp, formattedExp;
				GET_TEXT(IDC_CLIENT_TAG, exp); 
				
				verTagExp = RegexpHandler::formatRegExp(exp, params);
				formattedExp = verTagExp;
				string::size_type j = formattedExp.find("%[version]");
				if(j != string::npos) {
					formattedExp.replace(j, 10, ".*");
					version = RegexpHandler::getVersion(verTagExp, text);
					GET_TEXT(IDC_CLIENT_VERSION, versionExp)
				}
				string temp = RegexpHandler::matchExp(formattedExp, text);
				if(temp.find(STRING(S_MISSMATCH)) != string::npos||temp.find(STRING(S_INVALID)) != string::npos){
					MessageBox(temp.c_str(), CSTRING(REGEX_TESTER), MB_OK);
					return 0;
				}
				if(version.empty()) {
					MessageBox(CTSTRING(S_MATCH), CSTRING(REGEX_TESTER), MB_OK);
					return 0;
				} else {
					MessageBox(RegexpHandler::matchExp(versionExp, version).c_str(), CSTRING(REGEX_TESTER), MB_OK);
					//Sulan start svn93
					// http://www.imperialnet.org/forum/viewtopic.php?p=9844#9844
					return 0;
					//End
				}
			}
		case DESCRIPTION: 
			{
				GET_TEXT(IDC_CLIENT_EXTENDED_TAG, exp);
				string::size_type j = exp.find("%[version2]");
				if(j != string::npos) {
					exp.replace(j, 11, ".*");
				}
				break;
			}
		case LOCK:	GET_TEXT(IDC_CLIENT_LOCK, exp);	break;
		case PK: 
			{
				string version = "";
				string versionExp = "";
				GET_TEXT(IDC_CLIENT_PK, exp);
				string formattedExp = exp;
				string::size_type j = exp.find("%[version]");
				if(j != string::npos) {
					formattedExp.replace(j, 10, ".*");
					version = RegexpHandler::getVersion(exp, text);
					GET_TEXT(IDC_CLIENT_VERSION, versionExp)
				}

				string temp = RegexpHandler::matchExp(formattedExp, text);
				if(temp.find(STRING(S_MISSMATCH)) != string::npos||temp.find(STRING(S_INVALID)) != string::npos){
					MessageBox(temp.c_str(), CSTRING(REGEX_TESTER), MB_OK);
					return 0;
				}
				if(version.empty()) {
					MessageBox(CTSTRING(S_MATCH), CTSTRING(REGEX_TESTER), MB_OK);
					return 0;
				} else {
					MessageBox(RegexpHandler::matchExp(versionExp, version).c_str(), CSTRING(REGEX_TESTER), MB_OK);
					//Sulan start svn93
					// http://www.imperialnet.org/forum/viewtopic.php?p=9844#9844
					return 0;
					//End
				}
			}break;
		case SUPPORTS:	GET_TEXT(IDC_CLIENT_SUPPORTS, exp);	break;
		case TESTSUR:	GET_TEXT(IDC_CLIENT_TESTSUR_RESPONSE, exp);	break;
		case COMMANDS:	GET_TEXT(IDC_CLIENT_USER_CON_COM, exp);	break;
		case STATUS:	GET_TEXT(IDC_CLIENT_STATUS, exp);	break;
		case CONNECTION:	GET_TEXT(IDC_CLIENT_CONNECTION, exp);	break;
		default: dcdebug("We shouldn't be here!\n");
	}

	MessageBox(RegexpHandler::matchExp(exp, text).c_str(), CTSTRING(REGEX_TESTER), MB_OK);
	return 0;
}
//Sulan start svn93
void ClientProfileDlg::phraseAddLine() {

	tstring param; 
	TCHAR buf[BUF_LEN];
	GET_TEXT(IDC_ADD_LINE, param);

	StringTokenizer<string> t(param, ';');
	StringList& sl = t.getTokens();
	if(sl.size() != 18){
		MessageBox(CTSTRING(INCORRECT_PARAMS), CTSTRING(A_ERROR), MB_OK);
	return;
	}

	name = sl[0];
	version = sl[1];
	tag = sl[2];
	extendedTag = sl[3];
	lock = sl[4];
	pk = sl[5];
	supports = sl[6];
	testSUR = sl[7];
	userConCom = sl[8];
	status = sl[9];
	connection = sl[10];
	cheatingDescription = sl[11];
	comment = sl[12];
	//BOOLS
	useExtraVersion = Util::toBool(sl[13]);
	checkMismatch = Util::toBool(sl[14]);
	recheck = Util::toBool(sl[15]);
	skipExtended = Util::toBool(sl[16]);
	skipExtended2 = Util::toBool(sl[17]);
}
//End
/**
 * @file
 * $Id: ClientProfileDlg.cpp 306 2005-08-20 15:49:52Z Pothead $
 */